package com.nail.api.customMake.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.nail.utils.PageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.nail.api.customMake.bean.Indent;
import com.nail.api.customMake.bean.Message;
import com.nail.api.customMake.service.IInDentService;
import com.nail.api.customMake.service.IMessageService;
import com.nail.api.user.bean.User;
import com.nail.client.RedisClient;
import com.nail.common.Constant;
import com.nail.utils.IPUtils;
import com.nail.utils.Result4API;
import com.nail.controller.BaseController;
@Controller
@RequestMapping("/api/message")
public class MessageController  extends BaseController{
	Logger logger = Logger.getLogger(this.getClass());

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	private RedisClient redisClient;

	@Autowired
	private IMessageService messageService;

	@PostMapping(value="/addMessage")
	@ResponseBody
	public Result4API addIndent(HttpServletRequest request,Message message,String token){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：addMessage" );

		if(message == null ||  StringUtils.isEmpty(message.getMsg()) || StringUtils.isEmpty(message.getAccepter()+"") ||StringUtils.isEmpty(message.getUserId()+"")){
			result.setCode(Constant.API_CODE_501);
			result.setMsg(Constant.API_MSG_501);
			return result;
		}

		User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
		message.setUserId(user.getId());
		message.setUserName(user.getUserName());
		message.setCreateTime(new Date());
		message.setIsRead(0);

		try {
			messageService.addMessage(message);
			result.setCode(200);
			result.setMsg(Constant.API_MSG_SUCCESS);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}



	@PostMapping(value="/setIsRead")
	@ResponseBody
	public Result4API setSort(HttpServletRequest request,String messageId,String token){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：setIsRead" );

		if(StringUtils.isEmpty(messageId)){
			result.setCode(Constant.API_CODE_501);
			result.setMsg(Constant.API_MSG_501);
			return result;
		}

		try {
			messageService.setIsRead(messageId);
			result.setCode(200);
			result.setMsg(Constant.API_MSG_SUCCESS);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}

	@PostMapping(value="/setIsReadAndGetData")
	@ResponseBody
	public Result4API setIsReadAndGetData(HttpServletRequest request,String messageId,String token,Integer pageSize,Integer pageNo){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：setIsReadAndGetData" );

		if(StringUtils.isEmpty(messageId)){
			result.setCode(Constant.API_CODE_501);
			result.setMsg(Constant.API_MSG_501);
			return result;
		}
		
		PageResult pageResult = new PageResult();
		if(pageNo != null){
			pageResult.setPageNo(pageNo);
		}
		if(pageSize != null){
			pageResult.setPageSize(pageSize);
		}

		try {
			User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
			List<Message> list = messageService.setIsReadAndGetData(messageId,user.getId(),pageResult.getBegin(),pageResult.getPageSize());
			result.setData(list);
			result.setCode(Constant.API_CODE_SUCCESS);
			result.setMsg(Constant.API_MSG_SUCCESS);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}

	@PostMapping(value="/getMessage")
	@ResponseBody
	public Result4API getMessage(HttpServletRequest request,String token,Integer pageSize,Integer pageNo){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getMessage" );

		if(StringUtils.isEmpty(token)){
			result.setCode(Constant.API_CODE_501);
			result.setMsg(Constant.API_MSG_501);
			return result;
		}
		User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
		PageResult pageResult = new PageResult();
		if(pageNo != null){
			pageResult.setPageNo(pageNo);
		}
		if(pageSize != null){
			pageResult.setPageSize(pageSize);
		}
		try {
			//管理员不过滤权限
			pageResult = messageService.getMessageByUserId(user.getRole().getRoleType() == 0 ? user.getId():null,pageResult);
			result.setData(pageResult);
			result.setCode(200);
			result.setMsg(Constant.API_MSG_SUCCESS);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}
	
	
	@PostMapping(value="/getMessageByCondition")
	@ResponseBody
	public Result4API getMessageByCondition(HttpServletRequest request,String token,Integer pageSize,Integer pageNo,String condition){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getMessageByCondition" );

		if(StringUtils.isEmpty(token)){
			result.setCode(Constant.API_CODE_501);
			result.setMsg(Constant.API_MSG_501);
			return result;
		}
		User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
		PageResult pageResult = new PageResult();
		if(pageNo != null){
			pageResult.setPageNo(pageNo);
		}
		if(pageSize != null){
			pageResult.setPageSize(pageSize);
		}
		try {
			//管理员不过滤权限
			pageResult = messageService.getMessageByCondition(user.getRole().getRoleType() == 0 ? user.getId():null, pageResult, condition);
			result.setData(pageResult);
			result.setCode(200);
			result.setMsg(Constant.API_MSG_SUCCESS);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}
}
